home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / BROWSERS / GLUESTIK.ZOO / config.c next >
Encoding:
C/C++ Source or Header  |  1996-08-24  |  4.3 KB  |  192 lines

  1. #include <osbind.h>
  2. #include <mintbind.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include "global.h"
  8.  
  9. #define SYSVAR_bootdev    (*((short *)0x446UL))
  10.  
  11.  
  12. unsigned char *file_data = 0;
  13. Var *vars = 0;
  14. static char file_name[160] = "A:\\STIK_CFG\\DEFAULT.CFG";
  15. static char stikdir_dat[] = "A:\\STIK_DIR.DAT";
  16. static char default_config[] = "ALLOCMEM = 50000\n";
  17.  
  18.  
  19. static int get_bootdrive(void)
  20. {
  21.   return (int)SYSVAR_bootdev;
  22. }
  23.  
  24. static int find_config_file(void)
  25. {
  26.   register long l;
  27.   register int n, fd, boot;
  28.   register char *s;
  29.  
  30.   boot = Supexec(get_bootdrive);
  31.   file_name[0] += boot;
  32.   stikdir_dat[0] += boot;
  33.  
  34.   if ((l = Fopen(stikdir_dat, 0)) < 0)
  35.     return 1;
  36.   fd = (int)(short)l;
  37.   if ((n = Fread(fd, sizeof(file_name), file_name)) < 0) {
  38.     Fclose(fd);
  39.     Cconws("Error reading STIK_DIR.DAT; using default path\r\n");
  40.     return 1;
  41.   }
  42.   if (n >= sizeof(file_name))
  43.     n = sizeof(file_name) - 1;
  44.   if (!(s = strchr(file_name, '\n')))
  45.     s = file_name + n;
  46.   if (s[-1] == '\r')
  47.     s--;
  48.   *s = 0;
  49.   /* Incompatibility:  The STiK docs imply that you can change the
  50.      config file name via STIK_DIR.DAT, but then say to include only the
  51.      new path.  I'm gonna handle it this way:  if the line read from
  52.      STIK_DIR.DAT has a trailing '/' or '\', assume it's a path and
  53.      append "DEFAULT.CFG"; otherwise, assume it's the whole file name
  54.      and return it untouched. */
  55.   if (s[-1] == '\\' || s[-1] == '/')
  56.     strcpy(s, "DEFAULT.CFG");
  57.   return 1;
  58. }
  59.  
  60. #define NEXT_LINE()    {while (*s) s++; while (!*s) s++;}
  61.  
  62. int load_config_file()
  63. {
  64.   register unsigned char *s;
  65.   unsigned long filesize;
  66.   long l;
  67.   int n, fd;
  68.   struct stat S;
  69.  
  70.   if (!find_config_file()) {    /* Okay, so I'm paranoid... ;) */
  71.     Cconws("Unable to find config file\r\n");
  72.     return -1;
  73.   }
  74.   if (Fxattr(0, file_name, &S)) {
  75.     Cconws("Config file ");
  76.     Cconws(file_name);
  77.     Cconws(" does not exist; using built-in defaults...\r\n");
  78.     
  79.     file_data = (unsigned char *)Mxalloc(sizeof(default_config), 3);
  80.     if (!file_data) {
  81.       Cconws("Cannot get memory for default config file\r\n");
  82.       return -1;
  83.     }
  84.     strcpy((char *)file_data, default_config);
  85.   } else {
  86.     filesize = S.st_size;
  87.     Cconws("Reading config file ");
  88.     Cconws(file_name);
  89.     Cconws("...\r\n");
  90.  
  91.     file_data = (unsigned char *)Mxalloc(filesize + 2, 3);
  92.     if (!file_data) {
  93.       Cconws("Cannot get memory to read config file\r\n");
  94.       return -1;
  95.     }
  96.  
  97.     if ((l = Fopen(file_name, 0)) < 0) {
  98.       Cconws("Error opening config file ");
  99.       Cconws(file_name);
  100.       Cconws("\r\n");
  101.       return -1;
  102.     }
  103.     fd = (int)(short)l;
  104.  
  105.     s = file_data;
  106.     while ((n = Fread(fd, filesize, s)) > 0)
  107.       s += n;
  108.     if (n < 0) {
  109.       Cconws("Error reading config file ");
  110.       Cconws(file_name);
  111.       Cconws("\r\n");
  112.       return -1;
  113.     }
  114.     Fclose(fd);
  115.     *s = 0;
  116.   }
  117.  
  118.   for (s = file_data, l = 0; *s; s++) {
  119.     if (*s == '\r')
  120.       *s = 0;
  121.     if (*s == '\n')
  122.       *s = 0, l++;
  123.   }
  124.   /* make sure last line gets counted */
  125.   if (s[-1] != '\0')
  126.     l++;
  127.  
  128.   vars = (Var *)Mxalloc((l + 1) * sizeof(Var), 3);
  129.   if (!vars) {
  130.     Cconws("Cannot get memory for config vars\r\n");
  131.     return -1;
  132.   }
  133.   s = file_data;
  134.   n = 0;
  135.   while (s < file_data + filesize && n < l) {
  136.     int has_equals = 0;
  137.  
  138.     while (*s && isspace(*s))
  139.       s++;
  140.     if (!*s || *s == '#') {
  141.       NEXT_LINE();
  142.       continue;
  143.     }
  144.     vars[n].name = (char *)s;
  145.     while (*s && !isspace(*s) && *s != '=')
  146.       s++;
  147.     if (!*s) {
  148.       vars[n++].value = "1";
  149.       continue;
  150.     }
  151.     if (*s == '=')
  152.       has_equals = 1;
  153.     *s++ = 0;
  154.     while (*s && isspace(*s))
  155.       s++;
  156.     if (!has_equals && *s == '=') {
  157.       has_equals = 1;
  158.       s++;
  159.       while (*s && isspace(*s))
  160.     s++;
  161.     }
  162.     if (*s)
  163.       vars[n].value = (char *)s;
  164.     else if (has_equals)
  165.       vars[n].value = "0";
  166.     else
  167.       vars[n].value = "1";
  168.     n++;
  169.     NEXT_LINE();
  170.   }
  171.   vars[n].name = vars[n].value = 0;
  172.   return 0;
  173. }
  174.  
  175. char* do_getvstr(char *var)
  176. {
  177.   register int n;
  178.  
  179.   for (n = 0; vars[n].name; n++) {
  180.     if (stricmp(var, vars[n].name) == 0) {
  181. #ifdef DEBUG
  182.       debug("sssss", "getvstr(\"", var, "\") returns \"", vars[n].value, "\"");
  183. #endif
  184.       return vars[n].value;
  185.     }
  186.   }
  187. #ifdef DEBUG
  188.   debug("sss", "getvstr(\"", var, "\") returns \"0\"");
  189. #endif
  190.   return "0";
  191. }
  192.